ggplot()

FSH 507 Fall 2019

Amanda Warlick

2019-09-29

Benefits of ggplot()

The Basics

The Basics

head(data)
##        site    lat      long year week mo yday PG_count     v  temp  mins
## 1 Cliffside 48.368 -122.6697 2017   32  8  220       15 -0.63  0.48 -1.25
## 2 Cliffside 48.368 -122.6697 2017   33  8  228       15 -1.24  0.90  1.06
## 3 Cliffside 48.368 -122.6697 2016   34  8  235       16 -1.12  0.93 -0.48
## 4 Cliffside 48.368 -122.6697 2014   27  7  188       14 -1.66  0.35  1.27
## 5 Cliffside 48.368 -122.6697 2010   27  7  184       23 -2.21 -0.73 -0.16
## 6 Cliffside 48.368 -122.6697 2008   31  8  217       23 -0.94 -1.23 -0.80

Scatterplot

ggplot(data, aes(yday, temp)) + 
  geom_point()

Customize

ggplot(data, aes(yday, temp)) + 
  geom_point(color = 'blue') +
  xlab('Year Day') + ylab('Temperature') +
  theme_classic() #other simple versions include theme_bw()

Customize

ggplot(data, aes(yday, temp)) + 
  geom_point(aes(color = factor(mo))) +
  xlab('Year Day') + ylab('Temperature') +
  theme_classic() #other simple versions include theme_bw()

Customize

ggplot(data, aes(yday, temp)) + 
      #try grouping color by year or site
  geom_point(aes(color = factor(mo))) + 
  #try adding size = year inside aes()
  # geom_point(aes(color = factor(mo), size = year, alpha = 0.2), show.legend = F) +
  xlab('Year Day') + ylab('Temperature') +
  theme(legend.title = element_blank(), #play around with turning these elements on and off one at a time
        plot.background = element_rect(), 
        panel.background = element_rect(fill = 'white'), #color background of plot
        panel.border = element_rect(fill = NA), #border of plot
        panel.grid = element_blank(),
        legend.key = element_blank(),
        legend.position = 'top') +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors) 

Customize - class exercise

ggplot(data, aes(yday, temp)) + 
  geom_point(aes(color = factor(mo))) + 
  xlab('Year Day') + ylab('Temperature') +
  theme(legend.title = element_blank(), #play around with turning these elements on and off one at a time
        plot.background = element_rect(), 
        panel.background = element_rect(fill = 'white'), #color background of plot
        panel.border = element_rect(fill = NA), #border of plot
        panel.grid = element_blank(),
        legend.key = element_blank(),
        legend.position = 'top',
        axis.text = element_text(size = 10)) +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors)

Other plot types

ggplot(data, aes(factor(mo), temp)) + 
  geom_boxplot(aes(color = factor(mo))) + #try geom_violin()!
  xlab('Month') + ylab('Temperature') +
  my_theme(legend.position = 'top') +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors)

Faceting

ggplot(data, aes(factor(mo), temp)) + 
  geom_boxplot(aes(color = factor(mo))) + 
  xlab('Month') + ylab('Temperature') +
  my_theme(legend.position = 'top',
           strip.background = element_blank()) +
  facet_wrap(~year) +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors)

Multiple geom layers

ggplot(data_means %>% filter(site == 'Cliffside'), 
       aes(factor(year), temp, col = factor(mo), group = factor(mo))) + 
  geom_point() + 
  geom_line() +
  xlab('') + ylab('Temperature') +
  my_theme(legend.position = 'top',
           strip.background = element_blank()) +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors)

Stat layers

ggplot(data_means %>% filter(site == 'Cliffside'), #look at different sites, or facet by site below
       aes(factor(year), temp, col = factor(mo), group = factor(mo))) + 
  geom_point(size = 0.8) + 
  geom_line(linetype = 'dashed') + #se = F, method = 'lm'
  geom_smooth(aes(fill = factor(mo)), alpha = 0.2, show.legend = F) +
  #or, instead, use geom_ribbon() if have own 95% CI
  #geom_ribbon(aes(mean = mean, ymin = lower, ymax = upper)) +
  xlab('') + ylab('Temperature') +
  # facet_wrap(~site) +
  my_theme(legend.position = 'top') +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors) +
  scale_fill_manual(values = mycolors)

Stat layers

ggplot(data_means, 
       aes(tide, count, group = year)) + 
  geom_point(size = 0.8) +
  # geom_line(linetype = 'dashed') + #se = F, method = 'lm'
  geom_smooth(method = 'lm') +
  xlab('Tide Height') + ylab('Seabird count') +
  facet_wrap(~year, scales = 'free_y')+
  my_theme(legend.position = 'top',
           strip.background = element_blank()) +
  scale_fill_manual(values = mycolors)

Complications

gganimate for gifs

ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  my_theme() +
  # gganimate 
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')

Map-making

pnw_state_outline <- map_data("state", region = c("oregon", "washington")) 

ggplot(pnw_state_outline) +
geom_polygon(aes(x = long, y = lat, group = group), fill = "grey93", color = "grey50", size = 0.2) + 
  coord_fixed(1, ylim = c(47, 49.15), xlim = c(-125, -121)) + #closer in
# coord_fixed(1, ylim = c(45, 49.15), xlim = c(-125, -116)) + #adjust these to zoom in/out; whole state
  geom_point(data = data, aes(long, lat, color = temp)) +
  xlab('') + ylab('') +
  theme_classic() +
   theme(
    axis.line = element_blank(), 
    # axis.title = element_blank(),  #put these two lines in if don't want axis labels/ticks
    # axis.ticks = element_blank()
    strip.text = element_text(size = 8),
    strip.background = element_blank(),
    legend.title = element_blank(),
    legend.text = element_text(size = 16),
    panel.border = element_rect(colour = "black", fill = NA, size = 1))

Map-making

us_pop <- map_data('state') %>%
  merge(uspop_data, by = 'region')

ggplot(us_pop, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = population)) +
  coord_fixed(1.3) + scale_fill_gradient(trans = 'log10') 

Power of cowplot

a <- ggplot(us_pop, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = population)) +
  coord_fixed(1.3) + scale_fill_gradient(trans = 'log10') 

b <- ggplot(data_means %>% filter(site == 'Cliffside'), 
       aes(factor(year), temp, col = factor(mo), group = factor(mo))) + 
  geom_point() + 
  geom_line() +
  xlab('') + ylab('Temperature') +
  my_theme(legend.position = 'top',
           strip.background = element_blank()) +
  scale_color_manual(labels = c('May', 'June', 'July', 'August'), values = mycolors)

plot_grid(a, b, ncol = 2)

Exercise 1: explore density plots

Exercise 2: Make your own

Resources

The ggplot2 cheatsheet

Examples of manipulating everything

A master list of (sometimes ugly, but mind-opening) ggplots

Helpful outline of functionality per [specific geoms] (https://ggplot2.tidyverse.org/reference/)

Examples from R for Data Science

Nice color palettes